# -*- coding: utf-8 -*-
# @Time    : 2018/2/4 14:51
# @Author  : Soft
# @File    : models.py
from peewee import *
from playhouse.shortcuts import model_to_dict
from playhouse.pool import PooledMySQLDatabase

db = PooledMySQLDatabase(database='ck_check', max_connections=30, stale_timeout=300, timeout=20,
                         host='47.114.53.183', user='root', password='123456', charset='utf8')


# dbpath = str(path.join(path.dirname(__file__), 'qiaodao.db'))

# check_same_thread=False 避免检测连接池中的连接不是来自同一个线程导致sqlite不能用 默认开启
# db = PooledSqliteExtDatabase(database=dbpath, max_connections=30, stale_timeout=300, timeout=30,
#                              check_same_thread=False)


class BaseModel(Model):
    id = AutoField(primary_key=True, default=0)

    @classmethod
    def find_by_id(cls, pk):
        try:
            return cls.get_by_id(pk=pk).to_dict()
        except:
            return cls().to_dict()

    def to_dict(self):
        return model_to_dict(self)

    class Meta:
        database = db


class Location(BaseModel):
    location_name = CharField(max_length=50, default='', unique=True)
    lng = FloatField(default=0.0)
    lat = FloatField(default=0.0)


class Admin(BaseModel):
    ad_name = CharField(max_length=50, null=True)
    ad_pass = CharField(max_length=50, null=True)
    ad_tel = CharField(max_length=50, null=True)
    end_time = CharField(max_length=50, null=True)
    teacher_name = CharField(max_length=50, null=True)


class Student(BaseModel):
    stu_name = CharField(max_length=50, null=True)
    stu_pass = CharField(max_length=50, null=True)
    stu_email = CharField(max_length=50, null=True)
    stu_tel = CharField(max_length=50, null=True)
    stu_class = CharField(max_length=50, null=True)
    stu_id = CharField(max_length=50, null=True)  # 使用保存学生账号
    stu_reg_time = CharField(max_length=50, null=True)


class Class(BaseModel):
    k_name = CharField(max_length=50, null=True)
    k_time = CharField(max_length=50, null=True)
    k_teacher = CharField(max_length=50, null=True)
    k_work = TextField(null=True, default="work request")
    k_work_endtime = CharField(max_length=50, null=True)
    k_stu_num = IntegerField(default=0)
    k_id = CharField(max_length=50, null=True)  # 不使用
    k_status = IntegerField(default=0)
    location = ForeignKeyField(Location, column_name='l_id')


class Record(BaseModel):
    k = ForeignKeyField(Class, column_name='k_id')
    stu = ForeignKeyField(Student, column_name='stu_id')
    rd_status = IntegerField(default=0)
    rd_time = CharField(max_length=50, null=True)
    explain = TextField(default='')
    deviation = FloatField(default=0.0)  # 位置偏差


# class R_Dim(Model):
#     code = IntegerField()
#     value = CharField(max_length=50, null=True)
#
#     class Meta:
#         database = db
#         db_table = 'dim_record_status'


class Work(BaseModel):
    k = ForeignKeyField(Class, column_name='k_id')
    stu = ForeignKeyField(Student, column_name='stu_id')
    work_status = IntegerField()
    work_score = IntegerField(default=-1)
    work_uptime = CharField(max_length=50, null=True)
    work_url = CharField(max_length=100, null=True)
    version = IntegerField(default=0)


# class W_Dim(Model):
#     code = IntegerField()
#     value = CharField(max_length=50, null=True)
#
#     class Meta:
#         database = db
#         db_table = 'dim_work_status'


# 自习课表 暂时只有一次自习 只读
class z_study(BaseModel):
    name = CharField(max_length=50, default='自习')
    start_time = CharField(max_length=50)
    end_time = CharField(max_length=50)


class z_study_record(BaseModel):
    stu = ForeignKeyField(Student, column_name='stu_id')
    status = IntegerField(default=0)  # 0没来 1来了
    datetime = CharField(max_length=50, null=True)


class RPage:
    page = 1  # 当前页数
    limit = 5  # 每页显示多少
    pages = 0  # 总页数
    total = 0  # 总记录数
    result = []  # 本页数据

    def __init__(self):
        pass

    def to_dict(self):
        self.pages = int((self.total / self.limit) + (1 if self.total % self.limit else 0))
        front = 4
        back = 5
        if self.pages <= 10:
            pageNums = [x for x in range(1, self.pages + 1)]
        else:
            if self.page <= front + 1:
                pageNums = [x for x in range(1, 11)]
            elif self.pages < self.page + back:
                add = back - (self.pages - self.page) + front
                pageNums = [x for x in range(self.page - add, self.pages + 1)]
            else:
                pageNums = [x for x in range(self.page - front, self.page + back + 1)]
        return dict(page=self.page, limit=self.limit, pages=self.pages,
                    total=self.total, result=self.result, pageNums=pageNums)

Page = RPage()
